package com.coder.baseapp.mvp.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.coder.baseapp.App;
import com.coder.baseapp.injecter.components.ActivityComponent;
import com.coder.baseapp.injecter.components.DaggerActivityComponent;
import com.coder.baseapp.injecter.components.DaggerFragmentComponent;
import com.coder.baseapp.injecter.components.FragmentComponent;
import com.coder.baseapp.injecter.module.ActivityModule;
import com.coder.baseapp.injecter.module.FragmentModule;

import butterknife.ButterKnife;

/**
 * 基类
 *
 * @Author: Tua.CoderGui
 * *
 * @Date: 2017/3/13
 * *
 * Copyright (c) 2017 All Rights Reserved.
 */
public abstract class BaseFragment extends Fragment implements ILayoutControl {

    protected boolean isVisible = false;

    protected boolean isFristVisible = true;

    protected boolean isPrepared = false;

    protected View rootView = null;

    private FragmentComponent mComponent;

    private Bundle mSavedInstanceState = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);     
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(mSavedInstanceState = savedInstanceState);

        ButterKnife.bind(this, rootView);

        initComponent();

        initInjector(mComponent);

        isPrepared = true;

        lazyLoad(savedInstanceState);

    }

    /**
     * 初始化component
     */
    protected void initComponent() {
        mComponent = DaggerFragmentComponent.builder()
                .applicationComponent(App.getAppComponent())
                .fragmentModule(new FragmentModule(this))
                .build();
    }

    /**
     * Dagger 注入
     */
    protected abstract void initInjector(FragmentComponent component);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisible = isVisibleToUser) {
            lazyLoad(mSavedInstanceState);
        }
    }

    /**
     * lazy 懒加载.
     */
    protected void lazyLoad(Bundle savedInstanceState) {
        if (isPrepared && isVisible && isFristVisible) {
            initView(savedInstanceState);
            isFristVisible = false;
        }
    }

    @Override
    public void openActivity(Class<?> clazz) {
        openActivity(clazz, null);
    }

    @Override
    public void openActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz) {
        openActivityWithResult(clazz, Activity.RESULT_OK);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz, int code) {
        openActivityWithResult(clazz, code, null);
    }

    @Override
    public void openActivityWithResult(Class<?> clazz, int code, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, code);
    }

    @Override
    public void showToastLong(String txt) {
        Toast.makeText(getActivity(), txt, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToastShort(String txt) {
        Toast.makeText(getActivity(), txt, Toast.LENGTH_SHORT).show();
    }
}
